﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineJobPortal.Models;
using System.Data.Entity;
using System.Data;

namespace OnlineJobPortal.Models
{
    public class ResumeRepository : IResumeRepository
    {
        JobPortalEntities7 entityObj = new JobPortalEntities7();

        public List<PersonalInfo> SearchByInfoID(int userid)
        {
            var resumeObject = from j in entityObj.PersonalInfoes
                            where j.UserId == userid
                            select j;
            var list = resumeObject.ToList();
            return list;
        }
        public List<Academic> SearchByAcademicsID(int userid)
        {
            var resumeObject = from j in entityObj.Academics
                            where j.UserID == userid
                            select j;
            var list = resumeObject.ToList();
            return list;
        }
        public List<Experience> SearchByExperienceID(int userid)
        {
            var resumeObject = from j in entityObj.Experiences
                               where j.UserID == userid
                               select j;
            var list = resumeObject.ToList();
            return list;
        }
        public List<Honour> SearchByHonourID(int userid)
        {
            var resumeObject = from j in entityObj.Honours
                               where j.UserID == userid
                               select j;
            var list = resumeObject.ToList();
            return list;
        }
        public List<Language> SearchByLanguageID(int userid)
        {
            var resumeObject = from j in entityObj.Languages
                               where j.UserID == userid
                               select j;
            var list = resumeObject.ToList();
            return list;
        }
        public List<Project> SearchByProjectID(int userid)
        {
            var resumeObject = from j in entityObj.Projects
                               where j.UserID == userid
                               select j;
            var list = resumeObject.ToList();
            return list;
        }
        public List<Reference> SearchByReferenceID(int userid)
        {
            var resumeObject = from j in entityObj.References
                               where j.UserID == userid
                               select j;
            var list = resumeObject.ToList();
            return list;
        }
        public List<Skill> SearchBySkillsID(int userid)
        {
            var resumeObject = from j in entityObj.Skills
                               where j.UserID == userid
                               select j;
            var list = resumeObject.ToList();
            return list;
        }
        public List<TargetJob> SearchByTargetJobID(int userid)
        {
            var resumeObject = from j in entityObj.TargetJobs
                               where j.UserID == userid
                               select j;
            var list = resumeObject.ToList();
            return list;
        }
        public void DeleteResume(int id)
        {
            var list = entityObj.PersonalInfoes.ToList();
            foreach (var obj in list)
            {
                if (obj.UserId == id)
                {
                    PersonalInfo p = entityObj.PersonalInfoes.First(x => x.UserId == id);
                    var infodata = entityObj.PersonalInfoes;
                    infodata.Remove(p);
                    entityObj.SaveChanges();
                }
            }
            var alist = entityObj.Academics.ToList();
            foreach (var obj in alist)
            {
                if (obj.UserID == id)
                {
                    Academic a = entityObj.Academics.First(x => x.UserID == id);
                    var acdemydata = entityObj.Academics;
                    acdemydata.Remove(a);
                    entityObj.SaveChanges();
                }

            }
            var elist = entityObj.Experiences.ToList();
            foreach (var obj in elist)
            {
                if (obj.UserID == id)
                {
                    Experience exp = entityObj.Experiences.First(x => x.UserID == id);
                    var expdata = entityObj.Experiences;
                    expdata.Remove(exp);
                    entityObj.SaveChanges();
                }
            }
            var hlist = entityObj.Honours.ToList();
            foreach (var obj in hlist)
            {
                if (obj.UserID == id)
                {
                    Honour honour = entityObj.Honours.First(x => x.UserID == id);
                    var honourdata = entityObj.Honours;
                    honourdata.Remove(honour);
                    entityObj.SaveChanges();
                }
            }

            var Llist = entityObj.Languages.ToList();
            foreach (var obj in Llist)
            {
                if (obj.UserID == id)
                {
                    Language langdata = entityObj.Languages.First(x => x.UserID == id);
                    var lang = entityObj.Languages;
                    lang.Remove(langdata);
                    entityObj.SaveChanges();
                }
            }

            var reflist = entityObj.References.ToList();
            foreach (var obj in reflist)
            {
                if (obj.UserID == id)
                {
                    Reference refer = entityObj.References.First(x => x.UserID == id);
                    var referdata = entityObj.References;
                    referdata.Remove(refer);
                    entityObj.SaveChanges();
                }
            }

            var slist = entityObj.Skills.ToList();
            foreach (var obj in slist)
            {
                if (obj.UserID == id)
                {
                    Skill skill = entityObj.Skills.First(x => x.UserID == id);
                    var skilldata = entityObj.Skills;
                    skilldata.Remove(skill);
                    entityObj.SaveChanges();
                }
            }

            var tlist = entityObj.TargetJobs.ToList();
            foreach (var obj in tlist)
            {
                if (obj.UserID == id)
                {
                    TargetJob tjob = entityObj.TargetJobs.First(x => x.UserID == id);
                    var tjobdata = entityObj.TargetJobs;
                    tjobdata.Remove(tjob);
                    entityObj.SaveChanges();
                }
            }

            var plist = entityObj.Projects.ToList();
            foreach (var obj in plist)
            {
                if (obj.UserID == id)
                {
                    Project project = entityObj.Projects.First(x => x.UserID == id);
                    var projectdata = entityObj.Projects;
                    projectdata.Remove(project);
                    entityObj.SaveChanges();
                }
            }
           
        }
        public PersonalInfo SearchByInfo(int userid)
        {
            var list = entityObj.PersonalInfoes.ToList();
            foreach (var obj in list)
            {
                if (obj.UserId == userid)
                {
                    PersonalInfo pInfo = entityObj.PersonalInfoes.First(x => x.UserId == userid);
                    return pInfo;
                }
            }
            return null;
        }
        public Academic SearchByAcademics(int userid)
        {
             var list = entityObj.Academics.ToList();
             foreach (var obj in list)
             {
                 if (obj.UserID == userid)
                 {
                     Academic acdemy = entityObj.Academics.First(x => x.UserID == userid);
                     return acdemy;
                 }
             }
            return null;
        }
        public Experience SearchByExperience(int userid)
        {
            var list = entityObj.Experiences.ToList();
            foreach (var obj in list)
            {
                if (obj.UserID == userid)
                {
                    Experience exp = entityObj.Experiences.First(x => x.UserID == userid);
                        return exp;
                }
            }
            return null;
        }
        public Honour SearchByHonour(int userid)
        {
            var list = entityObj.Honours.ToList();
            foreach (var obj in list)
            {
                if (obj.UserID == userid)
                {
                    Honour honour = entityObj.Honours.First(x => x.UserID == userid);
                        return honour;
                }
            }
            return null;
        }
        public Language SearchByLanguage(int userid)
        {
            var list = entityObj.Languages.ToList();
            foreach (var obj in list)
            {
                if (obj.UserID == userid)
                {
                    Language lang = entityObj.Languages.First(x => x.UserID == userid);
                        return lang;
                }
            }
            return null;
        }
        public Project SearchByProject(int userid)
        {
            var list = entityObj.Projects.ToList();
            foreach (var obj in list)
            {
                if (obj.UserID == userid)
                {
                    Project project = entityObj.Projects.First(x => x.UserID == userid);
                        return project;
                }
            }
            return null;
        }
        public Reference SearchByReference(int userid)
        {
            var list = entityObj.References.ToList();
            foreach (var obj in list)
            {
                if (obj.UserID == userid)
                {
                    Reference refer = entityObj.References.First(x => x.UserID == userid);
                        return refer;
                }
            }
            return null;
        }
        public Skill SearchBySkills(int userid)
        {
            var list = entityObj.Skills.ToList();
            foreach (var obj in list)
            {
                if (obj.UserID == userid)
                {
                    Skill skill = entityObj.Skills.First(x => x.UserID == userid);
                        return skill;
                }
            }
            return null;
        }
        public TargetJob SearchByTargetJob(int userid)
        {
            var list = entityObj.TargetJobs.ToList();
            foreach (var obj in list)
            {
                if (obj.UserID == userid)
                {
                    TargetJob tjob = entityObj.TargetJobs.First(x => x.UserID == userid);
                        return tjob;
                }
            }
            return null;
        }

        public void UpdateInfo(PersonalInfo obj)
        {
            var infodata = entityObj.PersonalInfoes;
                entityObj.Entry(obj).State = EntityState.Modified;
                entityObj.SaveChanges();
            
        }
        public void UpdateAcademic(Academic obj)
        {
                var acdmydata = entityObj.Academics;
                entityObj.Entry(obj).State = EntityState.Modified;
                entityObj.SaveChanges();
            
        }
        public void UpdateExperience(Experience obj)
        {
            var infodata = entityObj.Experiences;
                entityObj.Entry(obj).State = EntityState.Modified;
                entityObj.SaveChanges();
            
        }
        public void UpdateHonour(Honour obj)
        {
            var infodata = entityObj.Honours;
                entityObj.Entry(obj).State = EntityState.Modified;
                entityObj.SaveChanges();
        }
        public void UpdateLanguage(Language obj)
        {
            var infodata = entityObj.Languages;
                entityObj.Entry(obj).State = EntityState.Modified;
                entityObj.SaveChanges();
            
        }
        public  void UpdateProject(Project obj)
        {
            var infodata = entityObj.Projects;
                entityObj.Entry(obj).State = EntityState.Modified;
                entityObj.SaveChanges();
            
        }
        public  void UpdateReference(Reference obj)
        {
            var infodata = entityObj.References;
            entityObj.Entry(obj).State = EntityState.Modified;
                entityObj.SaveChanges();
           
        }
        public void UpdateSkills(Skill obj)
        {
            var infodata = entityObj.Skills;
                entityObj.Entry(obj).State = EntityState.Modified;
                entityObj.SaveChanges();
            
        }
        public void UpdateTargetJob(TargetJob obj)
        {
            var infodata = entityObj.TargetJobs;
            entityObj.Entry(obj).State = EntityState.Modified;
            entityObj.SaveChanges();
        }

        public void AddInfo(PersonalInfo obj)
        {
            var infodata = entityObj.PersonalInfoes;
            infodata.Add(obj);
            entityObj.SaveChanges();
        }
        public void AddAcademic(Academic obj)
        {
            var infodata = entityObj.Academics;
            infodata.Add(obj);
            entityObj.SaveChanges();
        }
        public void AddExperience(Experience obj)
        {
            var infodata = entityObj.Experiences;
            infodata.Add(obj);
            entityObj.SaveChanges();
        }
        public void AddHonour(Honour obj)
        {
            var infodata = entityObj.Honours;
            infodata.Add(obj);
            entityObj.SaveChanges();
        }
        public void AddLanguage(Language obj)
        {
            var infodata = entityObj.Languages;
            infodata.Add(obj);
            entityObj.SaveChanges();
        }
        public void AddProject(Project obj)
        {
            var infodata = entityObj.Projects;
            infodata.Add(obj);
            entityObj.SaveChanges();
        }
        public void AddReference(Reference obj)
        {
            var infodata = entityObj.References;
            infodata.Add(obj);
            entityObj.SaveChanges();
        }
        public void AddSkills(Skill obj)
        {
            var infodata = entityObj.Skills;
            infodata.Add(obj);
            entityObj.SaveChanges();
        }
        public void AddTargetJob(TargetJob obj)
        {
            var infodata = entityObj.TargetJobs;
            infodata.Add(obj);
            entityObj.SaveChanges();
        }
    }
}